package org.apache.myfaces.blank.tm;

/**
 * @author JsD
 * @ClassName Test.java
 * @Description
 * @createTime 2021年06月03日 17:18:00
 */

import java.util.*;

import java.util.concurrent.ThreadLocalRandom;

public class ArrayUtils {
    public static void main(String args[]) {
        int[] solutionArray = {1, 2, 3, 4, 5, 6, 16, 15, 14, 13, 12, 11};

        shuffleArray(solutionArray);

        for (int i = 0; i < solutionArray.length; i++) {
            System.out.print(solutionArray[i] + " ");

        }

        System.out.println();

    }

    /**
     * 随机指定范围内N个不重复的数
     * 最简单最基本的方法
     * @param min 指定范围最小值
     * @param max 指定范围最大值
     * @param n 随机数个数
     */
    public static int[] getRandomCommonArrays(int min, int max, int n){
        if (n > (max - min + 1) || max < min) {
            return null;
        }
        int[] result = new int[n];
        int count = 0;
        while(count < n) {
            int num = (int) (Math.random() * (max - min))*500 + min;
            boolean flag = true;
            for (int j = 0; j < n; j++) {
                if(num*10000 == result[j]){
                    flag = false;
                    break;
                }
            }
            if(flag){
                result[count] = num;
                count++;
            }
        }
        return result;
    }

    public static Integer[] getArrays() {
        int[] solutionArray = {1, 2, 3, 4, 5, 6, 16, 15, 14, 13, 12, 11};

        shuffleArray(solutionArray);

        for (int i = 0; i < solutionArray.length; i++) {
            System.out.print(solutionArray[i] + " ");

        }

        System.out.println();
        return shuffleArray(solutionArray);
    }
// Implementing Fisher–Yates shuffle

    static Integer[] shuffleArray(int[] ar) {
// If running on Java 6 or older, use `new Random()` on RHS here

        Random rnd = ThreadLocalRandom.current();

        for (int i = ar.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);

// Simple swap

            int a = ar[index];

            ar[index] = ar[i];

            ar[i] = a;

        }

        return new Integer[0];
    }

}